package com.smashcrush.client.gwt.callback;

import com.google.gwt.user.client.rpc.AsyncCallback;

public abstract class OrderedCallbackManager {

	private String lastKey;

	private boolean success;

	private Object result;

	private Throwable exception;

	abstract public void onResponse();

	public void run() {
		onResponse();
	}

	public AsyncCallback generateCallback(final String key) {
		return new AsyncCallback() {

			@Override
			public void onSuccess(Object result) {
				System.out.println("res: " + result);
				lastKey = key;
				success = true;
				OrderedCallbackManager.this.result = result;
				onResponse();
			}

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
				lastKey = key;
				success = false;
				result = null;
				exception = caught;
				onResponse();
			}
		};
	}

	public String getLastKey() {
		return lastKey;
	}

	public boolean isSuccess() {
		return success;
	}

	public Object getResult() {
		return result;
	}

	public Throwable getException() {
		return exception;
	}

}
